package com.zadtek.shopmall.other;

import com.zadtek.shopmall.bean.OperateResponseBean3;
import com.zadtek.shopmall.bean.User;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class UserInfoManger {
    public interface FetchUserInfoListener{
        void success(User userInfo);
    }

    public interface UploadAvatarListener{
        void success(String url);
    }

    private static  User user = null;

    public static void fetch(final FetchUserInfoListener listener){
        if(user!=null && listener!=null){
            listener.success(user);
        }else{
            Map<String, Object> param = NetTool.newParams();
            NetTool.getApi().getUserInfo(param).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<BaseResponse<User>>() {
                        @Override
                        public void onData(BaseResponse<User> value) {
                            if(value.getCode() == 0 && value.getData()!=null ){
                                user = value.getData();
                                if(listener!=null){
                                    listener.success(user);
                                }
                            }
                        }
                    });
        }
    }

    public static void updateInfo(final User user, final FetchUserInfoListener listener){
        Map<String, Object> param = NetTool.newParams();
        param.put("user_name",user.getUser_name());
        param.put("sex",user.getSex());
        param.put("mobile_phone",user.getMobile_phone());
        param.put("email",user.getEmail());
        NetTool.getApi().updateUserInfo(param).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>() {
                @Override
                public void onData(BaseResponse<OperateResponseBean3> value) {
                    if(value.getCode() == 0 && value.getData()!=null){
                        if(value.getData().isSuccess()){
                            if(listener!=null){
                                listener.success(user);
                            }
                            ToastUtil.showToast("信息修改成功！");
                        }else{
                            ToastUtil.showToast(value.getData().getMessage());
                        }
                    }
                }
            });
    }

    public static void updateAvatar(String path, final UploadAvatarListener listener, BaseObserver.DialogHelper dialogHelper){
        NetTool.uploadAvatar(path).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean3>>(dialogHelper) {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean3> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(listener!=null) {
                                listener.success(value.getData().getImage());
                                user.setUser_picture(value.getData().getImage());
                            }else{
                                ToastUtil.showToast("修改失败"+value.getData().getMessage());
                            }
                        }

                    }
                });
    }
}
